package com.chandra.pos.customer.dao.entity;

import com.chandra.pos.customer.model.CustomerFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Admin on 8/15/14.
 */
public class CustomerRepositoryImpl implements CustomerRepositoryCustom {


    @PersistenceContext
    private EntityManager entityManager;


    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerRepositoryImpl.class);


    public List<CustomerEntity> search(CustomerFilter shipmentCriteriaSO) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<CustomerEntity> criteriaQuery = criteriaBuilder.createQuery(CustomerEntity.class);
        Root<CustomerEntity> from = criteriaQuery.from(CustomerEntity.class);
        criteriaQuery.select(from);


        List<Predicate> predicates = new ArrayList<Predicate>();

//        if(!StringUtils.isBlank(shipmentCriteriaSO.getCargoCategory())){
//            predicates.add(criteriaBuilder.equal(from.get("cargoCategory"), shipmentCriteriaSO.getCargoCategory()));
//        }
//        if(!StringUtils.isBlank(shipmentCriteriaSO.getDocumentType())){
//            predicates.add(criteriaBuilder.equal(from.get("documentType"), shipmentCriteriaSO.getDocumentType()));
//        }
//        if(!StringUtils.isBlank(shipmentCriteriaSO.getDocumentPrefix())){
//            predicates.add(criteriaBuilder.equal(from.get("documentPrefix"), shipmentCriteriaSO.getDocumentPrefix()));
//        }
//        if(!StringUtils.isBlank(shipmentCriteriaSO.getDocumentNumber())){
//            predicates.add(criteriaBuilder.equal(from.get("documentNumber"), shipmentCriteriaSO.getDocumentNumber()));
//        }
//        if(shipmentCriteriaSO.getMasterShipmemntId() != null && shipmentCriteriaSO.getMasterShipmemntId() > 0){
//            predicates.add(criteriaBuilder.equal(from.get("MasterShipmemntId"), shipmentCriteriaSO.getMasterShipmemntId()));
//        }
//        if(!StringUtils.isBlank(shipmentCriteriaSO.getCn38Number())){
//            predicates.add(criteriaBuilder.equal(from.get("cn38Number"), shipmentCriteriaSO.getCn38Number()));
//        }
//        if(!StringUtils.isBlank(shipmentCriteriaSO.getShipmentOrigin())){
//            predicates.add(criteriaBuilder.equal(from.get("shipmentOrigin"), shipmentCriteriaSO.getShipmentOrigin()));
//        }
//        if(!StringUtils.isBlank(shipmentCriteriaSO.getShipmentDestination())){
//            predicates.add(criteriaBuilder.equal(from.get("shipmentDestination"), shipmentCriteriaSO.getShipmentDestination()));
//        }
//        predicates.add(criteriaBuilder.equal(from.get("statusCode"), CGHConstants.SHIPMENT_STATUS_ACTIVE));
//
//        if (!StringUtils.isBlank(shipmentCriteriaSO.getCustomerType()) && !StringUtils.isBlank(shipmentCriteriaSO.getCustomerBranchCode()) ) {
//            Join<Shipment, ShipmentCustomer> customer = from.join("shipmentCustomers");
//
//            predicates.add(criteriaBuilder.equal(customer.<String>get("customerType"), shipmentCriteriaSO.getCustomerType()));
//
//            predicates.add(criteriaBuilder.equal(customer.<String>get("customerBranchCode"), shipmentCriteriaSO.getCustomerBranchCode()));
//
//        }
//
//        if (shipmentCriteriaSO.getCreatedStartDate() != null && shipmentCriteriaSO.getCreatedEndDate() != null) {
//            predicates.add(criteriaBuilder.between(from.<Date>get("createdDate"), shipmentCriteriaSO.getCreatedStartDate(), shipmentCriteriaSO.getCreatedEndDate()));
//        }

//        Long instanceId = InstanceConfiguration.getInstanceId();
//        LOGGER.info("instanceId" + instanceId);
//        predicates.add(criteriaBuilder.equal(from.get("instanceId"), instanceId));

        Predicate[] predicatesArray = predicates.toArray(new Predicate[predicates.size()]);
        criteriaQuery.where(criteriaBuilder.and(predicatesArray));

        TypedQuery<CustomerEntity> typedQuery = entityManager.createQuery(criteriaQuery);
        predicatesArray = null;

        // if(StringUtils.isBlank(shipmentCriteriaSO.getDocumentNumber())){
        LOGGER.error("Shipment is searched without documentNumber");
        typedQuery.setFirstResult(shipmentCriteriaSO.getPage());
        typedQuery.setMaxResults(shipmentCriteriaSO.getRows()); //Limiting maximum record that can be searched from here.
        //  }

        List<CustomerEntity> customerEntityList = typedQuery.getResultList();
        LOGGER.info(" Size of storageUnitLocationList : " + customerEntityList.size());
        return customerEntityList;
    }
}
